function [t, ic, v, err] = runNeuron (model, rdur, DT, waveform)
%runNeuron is a skeleton script for running a model using NEURON from matlab

t=[];
ic=[];
v=[];
err = 0;
FilePath = 'C:\NeuronModels\vcn_new';
hocFileName = 'bcell.hoc';
%if(nargin < 4) {
   %create fake stim file
%	istim = zeros(1500,1);
%	for i = 50:1050
%		istim(i) = -0.150;
%	end;
 %  waveform = istim;
%}

h = fopen('c:\NeuronModels\vcn_new\stim.dat', 'w');
fprintf(h, '%d\n', model); % model selected
fprintf(h, '%f\n', rdur); % duration of recording period (tstop)
fprintf(h, '%f\n', DT); % DT for vector to play
fprintf(h, '%d\n', length(waveform)); % number of points in istim
fprintf(h, '%d\n', waveform); % istim
fclose(h);

fullFileName = ['c:\nrn55\bin\nrniv.exe'] % where we expect to find NEURON
hocFile = [FilePath '\' hocFileName];

a=pwd;
cd (FilePath);
cmd = ['! ' fullFileName ' ' hocFile  ];
eval(slash4OS(cmd));
cd(a);

% now read the file and plot the results.
h = fopen('c:\NeuronModels\vcn_new\testing.txt', 'r');
model = fscanf(h, '%d',1);
sdt = fscanf(h, '%f', 1);
npts = fscanf(h, '%d', 1);
A = fscanf(h, '%g %g %g', [3 npts]);

t = A(1,:)';
ic = A(2,:)';
v = A(3,:)';
%figure;
%subplot(2,1,1);
%plot(t, v);
%subplot(2,1,2);
%plot(t,i);


%-----------------------------------------------------------------------------------
function strout = slash4OS(strin)
% strout = slash4OS(strin)
% Takes a string, containing paths, and converts any slashes to the correct
% form for the underlying operating system, automatically determining OS

if (computer=='PCWIN')
    newslash = '\';
    oldslash = '/';
else 
    newslash = '/';
    oldslash = '\';
end;

oldslashpos = findstr(strin,oldslash);
strin(oldslashpos) = ones(size(oldslashpos))*newslash;
strout = strin;
